package com.hzjq.encrypt.encrypt;

import com.hzjq.encrypt.util.ELog;

import java.net.URLDecoder;
import java.net.URLEncoder;

public enum EncryptUtils {
    INSTANCE;

    public static String KEY_BASE = "jadl12345678912345678912";

    public static String DE_KEY_BASE = "jadl12345678901234";

    public static String endocde(String inObj) {
        return endocde(inObj, getEncodeKey(KEY_BASE));
    }


    public static String endocde(String inObj, String key) {
        try {
            inObj = TripleDES_CRC.endocde(inObj, key);
            inObj = URLEncoder.encode(inObj, "UTF-8");
        } catch (Exception e) {
            inObj = "";
            e.printStackTrace();
            ELog.Companion.e("加密失败 =  " + e.getMessage());
        }
        return inObj;
    }

    public static String endocdeNoURL(String inObj, String key) {
        try {
            inObj = TripleDES_CRC.endocde(inObj, key);
        } catch (Exception e) {
            inObj = "";
            e.printStackTrace();
            ELog.Companion.e("加密失败 =  " + e.getMessage());
        }
        return inObj;
    }

    public static String decode(String content) {
        return decode(content, getDecodeKey(KEY_BASE));
    }

    public static String decode(String content, String key) {
        try {
            content = URLDecoder.decode(content, "UTF-8");
            content = TripleDES_CRC.decode(content, key);
        } catch (Exception e) {
            content = "";
            e.printStackTrace();
            ELog.Companion.e("解密失败 =  " + e.toString());
        }
        return content;
    }

    /**
     * 电子雷管解析
     *
     * @param content
     * @param key
     * @return
     */
    public static String decodeNoURL(String content, String key) {
        try {
            content = TripleDES_CRC.decode(content, key);
        } catch (Exception e) {
            content = "";
            e.printStackTrace();
            ELog.Companion.e("解密失败 =  " + e.toString());
        }
        return content;
    }

    private static String getDecodeKey(String str) {
        return "000000000000000000000000".substring(str.length()) + str;
    }

    private static String getEncodeKey(String str) {
        return str + "000000000000000000000000".substring(str.length());
    }

}
